/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.relations.adataset.entities;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;

import org.json.simple.JSONArray;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.reasoning.schema.Subclass;
import cn.edu.nju.ws.sview.wyge.relations.adataset.CustomDataset;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-7-28
 * 
 */
public class ClassFinder {
	/**
	 * 如果是-1没有作为主语，0没有type，其他有type
	 * 
	 * @param ds_type
	 * @param ds_id
	 * @param subject
	 * @return
	 */
	public static int find(String ds_type, int ds_id, String subject) {
		ArrayList<Integer> docs = null;
		if (ds_type.equals("custom")) {
			docs = CustomDataset.getURLIDsByCustomDatasetId(ds_id);
		} else if (ds_type.equals("uri")) {
			docs = new ArrayList<Integer>();
			docs.add(ds_id);
		}
		return find(docs, subject);
	}

	public static int find(ArrayList<Integer> docs, String subject) {
		int cls = -1;
		if (docs.size() > 0) {
			try {
				String c_string = "";
				for (int doc : docs) {
					c_string += doc + ",";
				}
				c_string = c_string.substring(0, c_string.length() - 1);

				Connection conn = DBConnectionFactory.getConnection();// .getFalconConnection();
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt
						.executeQuery(String
								.format("SELECT * FROM quadruple where doc_id in (%s) and s = \"%s\" limit 1",
										c_string, subject));
				if (rs.next()) {
					cls = 0;// has subject

					int typeid = 0;
					try {
						typeid = URIIDCache
								.getInstance()
								.getURIID(
										"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
										false);
					} catch (Throwable e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					String query = String.format(
							"SELECT o FROM quadruple where doc_id in (%s) and s = \"%s\" and p = \"u"
									+ typeid + "\"", c_string, subject);
					Statement stmt2 = conn.createStatement();
					ResultSet rs2 = stmt2.executeQuery(query
							+ " and o like \"u%\" limit 1");
					if (rs2.next()) {
						String object = rs2.getString(1);
						cls = Integer.parseInt(object.substring(1));
					}
					rs2.close();
					stmt2.close();
				}
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cls;
	}
	
	public static HashSet<Integer> getMinClass(ArrayList<Integer> docs, String subject) {
		HashSet<Integer> minClasses = null;
		if (docs.size() > 0) {
			try {
				String c_string = "";
				for (int doc : docs) {
					c_string += doc + ",";
				}
				c_string = c_string.substring(0, c_string.length() - 1);

				Connection conn = DBConnectionFactory.getConnection();// .getFalconConnection();
				int typeid = 0;
				try {
					typeid = URIIDCache
							.getInstance()
							.getURIID(
									"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
									false);
				} catch (Throwable e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				String query = String.format(
						"SELECT o FROM quadruple where doc_id in (%s) and s = \"%s\" and p = \"u"
								+ typeid + "\"", c_string, subject);
				Statement stmt2 = conn.createStatement();
				ResultSet rs2 = stmt2.executeQuery(query
						+ " and o like \"u%\"");
				HashSet<Integer> classes = new HashSet<Integer>();
				while (rs2.next()) {
					String object = rs2.getString(1);
					classes.add(Integer.parseInt(object.substring(1)));
				}
				rs2.close();
				stmt2.close();	
				conn.close();
				
				minClasses = Subclass.getInstance().getMinClasses(classes);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return minClasses;
	}
}
